<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>输出一个消息并且退出当前脚本</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.eval.html">« eval</a></li>
      <li style="float: right;"><a href="function.get-browser.html">get_browser »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.misc.html">杂项 函数</a></li>
    <li>输出一个消息并且退出当前脚本</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.exit" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">exit</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">exit</span> &mdash; <span class="dc-title">输出一个消息并且退出当前脚本</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.exit-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>exit</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$status</code><span class="initializer"> = ?</span></span>): <span class="type"><span class="type void">void</span></span></div>

  <div class="methodsynopsis dc-description"><span class="methodname"><strong>exit</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$status</code></span>): <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
   中止脚本的执行。
   尽管调用了 <span class="function"><strong>exit()</strong></span>，
   <a href="function.register-shutdown-function.html" class="link">Shutdown函数</a>
   以及 <a href="language.oop5.decon.html#language.oop5.decon.destructor" class="link">object destructors</a> 总是会被执行。
  </p>
  <p class="para">
   <code class="literal">exit</code> 是个语法结构，如果没有 <code class="parameter">status</code> 参数要传入，可以省略圆括号。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.exit-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">status</code></dt>

     <dd>

      <p class="para">
       如果 <code class="parameter">status</code> 是一个字符串，在退出之前该函数会打印
       <code class="parameter">status</code> 。
      </p>
      <p class="para">
       如果 <code class="parameter">status</code> 是一个 <span class="type">int</span>，该值会作为退出状态码，并且不会被打印输出。
       退出状态码应该在范围0至254，不应使用被PHP保留的退出状态码255。 
       状态码0用于成功中止程序。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.exit-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.exit-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3447">
    <p><strong>示例 #1 <span class="function"><strong>exit()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'/path/to/data-file'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;exit(</span><span style="color: #DD0000">"unable&nbsp;to&nbsp;open&nbsp;file&nbsp;(</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">)"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3448">
    <p><strong>示例 #2 <span class="function"><strong>exit()</strong></span> 状态码例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//exit&nbsp;program&nbsp;normally<br /></span><span style="color: #007700">exit;<br />exit();<br />exit(</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//exit&nbsp;with&nbsp;an&nbsp;error&nbsp;code<br /></span><span style="color: #007700">exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />exit(</span><span style="color: #0000BB">0376</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//octal<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3449">
    <p><strong>示例 #3 无论如何，Shutdown函数与析构函数都会被执行</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Foo<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__destruct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Destruct:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">__METHOD__&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'()'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">shutdown</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Shutdown:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">__FUNCTION__&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'()'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">register_shutdown_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'shutdown'</span><span style="color: #007700">);<br /><br />exit();<br />echo&nbsp;</span><span style="color: #DD0000">'This&nbsp;will&nbsp;not&nbsp;be&nbsp;output.'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
 <div class="cdata"><pre>
 Shutdown: shutdown()
 Destruct: Foo::__destruct()
 </pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.exit-notes">
  <h3 class="title">注释</h3>
  
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">因为是语言构造器而不是函数，不能被
<a href="functions.variable-functions.html" class="link">可变函数</a> 或者
<a href="functions.arguments.html#functions.named-arguments" class="link">命名参数</a> 调用。
</span>
</p></blockquote>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    该语法结构等同于 <span class="function"><a href="function.die.html" class="function">die()</a></span>。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.exit-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.register-shutdown-function.html" class="function" rel="rdfs-seeAlso">register_shutdown_function()</a> - 注册在关闭时执行的函数</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>